<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\Module as ModuleModel;
use think\facade\Log;

class Module extends CommonBase
{
    private $_moduleObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode = $websiteCode;
        $this->_moduleObj   = new ModuleModel([], $websiteCode);
    }

    public function getAllModule(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_moduleObj->getAllModule($where);
        } catch (\Exception $e) {
            logErr('读取所有模型失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows;
    }

    public function getModuleList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = [];
        $count = 0;
        try {
            $rows  = $this->_moduleObj->getModuleList($page, $limit, $where);
            $count = $this->_moduleObj->getModuleCount($where);
        } catch (\Exception $e) {
            logErr('读取模型列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addModule(array $data): array
    {
        unset($data['website_code']);
        try {
            return [self::_RESULT => $this->_moduleObj->addModule($data) && $this->_moduleObj->createTable($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加模型失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findModuleById(int $id): array
    {
        $module = [];
        try {
            $module = $this->_moduleObj->findModuleById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取模型详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($module)) {
            return [];
        }

        return $module;
    }

    public function updateModule(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_moduleObj->updateModule($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新模型信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteModule(int $id): array
    {
        try {
            $row = $this->findModuleById($id);
            if (empty($row)) {
                return [self::_RESULT => true, self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
            }

            return [self::_RESULT => $this->_moduleObj->deleteModule($id) && $this->_moduleObj->dropTable($row), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除模型失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function clearModuleCache(): bool
    {
        return $this->_moduleObj->clearModuleCache();
    }
}
